package com.theblind.privatenotes.action.anaction;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcs.branch.BranchData;
import com.intellij.vcs.branch.BranchStateProvider;
import com.intellij.vcsUtil.VcsUtil;
import com.theblind.privatenotes.action.ActionHandle;
import com.theblind.privatenotes.action.ActionHandleFactory;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

public class AddOrEditNoteAnAction extends AnAction {



    ActionHandle addHandle;
    ActionHandle editHandle;


    public static String currentBranchName = "";

    public static String currentProjectName = "";

    public static void setCurrentBranchName(String name) {
        if (name != null) {
            if (name.contains("/")) {
                name = name.replaceAll("/", "_");
            }
            currentBranchName = name;
        }
    }

    public static void setCurrentProjectName(String name) {
        if (name != null) {
            currentProjectName = name;
        }
    }

    public AddOrEditNoteAnAction() {
        addHandle = ActionHandleFactory.getActionHandle(ActionHandle.Operate.ADD);
        editHandle = ActionHandleFactory.getActionHandle(ActionHandle.Operate.EDIT);
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {

        Project project = CommonDataKeys.PROJECT.getData(anActionEvent.getDataContext());
        Editor editor = CommonDataKeys.EDITOR.getData(anActionEvent.getDataContext());
        VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(anActionEvent.getDataContext());

        AddOrEditNoteAnAction.setCurrentProjectName(project.getName());
        buildBranchName(project, virtualFile);
//        BranchStateProvider.EP_NAME.getExtensionList(project).get(1).getCurrentBranch(VcsUtil.getFilePath(virtualFile.getPath()));

        if (addHandle.isVisible(project, editor, virtualFile)) {
            addHandle.execute(project, editor, virtualFile);
            return;
        }
        editHandle.execute(project, editor, virtualFile);

    }

    public static void buildBranchName(Project project, VirtualFile virtualFile) {
        ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
        AbstractVcs activeVcs = vcsManager.getVcsFor(virtualFile);
        if (activeVcs == null) {
            // 当前项目没有建立git分支，则赋予默认空串
            setCurrentBranchName("");
            return;
        }
        FilePath filePath = VcsUtil.getFilePath(virtualFile.getPath());
        for (BranchStateProvider provider : BranchStateProvider.EP_NAME.getExtensionList(project)) {
            if (provider.getClass().getName().contains(activeVcs.getName())) {
                String currentBranchName = Optional.ofNullable(provider.getCurrentBranch(filePath)).map(
                        BranchData::getBranchName).map(String::toLowerCase).orElse(null);
                setCurrentBranchName(currentBranchName);
            }
        }
    }

    @Override
    public void update(@NotNull AnActionEvent anActionEvent) {
        super.update(anActionEvent);
        Project project = CommonDataKeys.PROJECT.getData(anActionEvent.getDataContext());
        Editor editor = CommonDataKeys.EDITOR.getData(anActionEvent.getDataContext());
        VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(anActionEvent.getDataContext());
        String title = ActionHandle.Operate.EDIT.getTitle();
        if (addHandle.isVisible(project, editor, virtualFile)) {
            title = ActionHandle.Operate.ADD.getTitle();
        }
        anActionEvent.getPresentation().setText(title);
    }


}
